package org.yuanqiframework.yuanqi.boot.sys.controller;

import org.springframework.web.bind.annotation.*;
import org.yuanqiframework.yuanqi.boot.auth.AuthService;
import org.yuanqiframework.yuanqi.web.controller.BaseController;
import org.yuanqiframework.yuanqi.web.entity.AjaxResult;
import org.yuanqiframework.yuanqi.boot.sys.dto.RoleUrlDTO;
import org.yuanqiframework.yuanqi.boot.sys.service.SysRoleService;
import org.yuanqiframework.yuanqi.boot.sys.vo.RoleVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sys/role")
public class RoleController extends BaseController {
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private AuthService authService;

    @GetMapping(value = "query")
    public AjaxResult<List<RoleVO>> query() {
        return AjaxResult.success(sysRoleService.queryRoleVoList());
    }


    @PostMapping(value = "addUrl")
    public AjaxResult<?> addUrl(@Valid @RequestBody RoleUrlDTO roleUrlDTO) {
        List<String> roleIds = roleUrlDTO.getRoleIds();
        List<String> urlIds = roleUrlDTO.getUrlIds();
        int i = sysRoleService.addUrl(roleIds, urlIds);
        if (i > 0) {
            authService.flushUrlRolesCache();
        }
        return toAjax(i);
    }


    @PostMapping(value = "removeUrl")
    public AjaxResult<?> removeUrl(@Valid @RequestBody RoleUrlDTO roleUrlDTO) {
        List<String> roleIds = roleUrlDTO.getRoleIds();
        List<String> urlIds = roleUrlDTO.getUrlIds();
        int i = sysRoleService.removeUrl(roleIds, urlIds);
        if (i > 0) {
            authService.flushUrlRolesCache();
        }
        return toAjax(i);
    }
}
